<?php

require_once($INCLUDE_PATH.'app/controller/InitController.php');
require_once ($INCLUDE_PATH . "/inc/checkfun.php");
require_once ($INCLUDE_PATH . "/include/WebService2Db.php");
function __autoload($class_name) {
    require_once APP_ROOT.'app/model/'.strtolower($class_name) . '.php';
}
class DomainController extends InitController {

    public function __construct() {
        parent::__construct();

    }


    public function actionInfo() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        if($_REQUEST['do']=="add" || empty($_REQUEST['do'])) {
            $do = "add_save";
            $do_msg = "添加新";
        }
        if($_REQUEST['do']=="edit") {
            $do = "edit_save";
            $do_msg = "修改";
        }
        $d_id = (int)$_REQUEST['id'];
        if($d_id>0) {
            Y::$tpl->set("d_id",$d_id);
            $domainInfo = DomainModel::getDomainInfo($d_id);
            Y::$tpl->set("domainInfo",$domainInfo);
        }
        Y::$tpl->set("do",$do);
        Y::$tpl->set("do_msg",$do_msg);

        if(count(explode("save",$_REQUEST['do']))>1) {
            if ($_REQUEST['do']=="add_save") {
                $s = array();
                $s['url'] = $_REQUEST['domain_name'];
                $s['created_time']=date("Y-m-d H:i:s");
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                $s['created_id']=$userInfo['id'];
                $result = DomainModel::saveDomainInfo($s);
            }
            if ($_REQUEST['do']=="edit_save") {
                $s = array();
                $s['url'] = $_REQUEST['domain_name'];
                $s['created_time']=date("Y-m-d H:i:s");

                $result = DomainModel::editDomainInfo($d_id,$s);
            }
            if ($result) {
                return self::sysMessage($do_msg."域名成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else {
                return self::sysMessage($do_msg."域名失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        //End for explode(save)
        }
    //echo PRE.'hhj';
    }
    public function actionList() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        if($_REQUEST['id']) {
            if ($_REQUEST['do'] == "del") {
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                //print_r($userType);
                if($userInfo["user_type"] == 2) {
                    $del = DomainModel::delDomain($_REQUEST['id']);
                }else {
                    die("<script LANGUAGE='javascript'>alert('对不起，您无权删除！');history.go(-1);</script>");
                }
            }
        }
        if($_REQUEST['p']) {
            $page = $_REQUEST['p'];
        }else {
            $page = 1;
        }
        $pageSize = 20;
        $sum = DomainModel::getListCount();
        if (ceil ( $sum / $pageSize ) <= $page) {
            $page = ceil ( $sum / $pageSize );
        }
        $liststart=$pageSize * ($page - 1);
        $result = DomainModel::getList($page,$liststart,$pageSize);
        $par = array();

        $showpage = Site_global::listPage($par, $sum, $page, $pageSize);
        Y::$tpl->set("showpage",$showpage);
        Y::$tpl->set("list",$result);
    }

}

?>
